package com.noble.meether.ui.diamond;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.noble.meether.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.utils.ScreenUtils;
import com.noble.meether.MeetUIShow;
import com.noble.meether.ui.diamond.bean.DiamondList;

/**
 * 我的钻石页面
 */
public class MyDiamondsAct extends BaseAppCompatActivity implements View.OnClickListener, RequestComplete, PObserver {
    private TextView tvDiamondSum;
    private RecyclerView rlvList;
    private MyDiamondAdapter mMyDiamondAdapter;
    private float toDpMutliple = 1; //根据屏幕密度获取屏幕转换倍数

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_diamond);
        initView();
    }

    @Override
    protected boolean isStatusBarLight() {
        return false;
    }

    private void initView() {
        initTitle();
        toDpMutliple = ScreenUtils.toDpMultiple(this);
        findViewById(R.id.diamond_declare).setOnClickListener(this);
        tvDiamondSum = findViewById(R.id.diamond_sum);
        rlvList = findViewById(R.id.recycler_view);
        rlvList.setLayoutManager(new GridLayoutManager(this, 3));
        rlvList.addItemDecoration(new ItemSpaces((int) (15 * toDpMutliple)));
        initData();
        MsgMgr.getInstance().attach(this);
    }

    private void initTitle() {
        setTitleBackground(getResources().getColor(R.color.transparent));
        setBackView(false);
        setTitle(getString(R.string.my_diamond), getResources().getColor(R.color.white));
        setTitleRight(getString(R.string.my_diamonds_explain), getResources().getColor(R.color.white), this);
    }

    private void initData() {
        tvDiamondSum.setText(ModuleMgr.getCenterMgr().getMyInfo().getDiamand() + "");
        mMyDiamondAdapter = new MyDiamondAdapter(this);
        rlvList.setAdapter(mMyDiamondAdapter);
        ModuleMgr.getCommonMgr().reqDiamondList(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.diamond_declare:
                UIShow.showMyDiamondsExplainAct(this);
                break;

            case R.id.base_title_right_txt:
                MeetUIShow.showDiamondDetailAct(this);
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqDiamondList) {
            if (!response.isOk()) return;

            DiamondList diamondList = (DiamondList) response.getBaseData();
            if (diamondList.getDiamondList().size() <= 0) return;
            mMyDiamondAdapter.setList(diamondList.getDiamondList());
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                tvDiamondSum.setText(ModuleMgr.getCenterMgr().getMyInfo().getDiamand() + "");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        private int space;

        public ItemSpaces(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.right = space / 2;
            outRect.left = space / 2;
            outRect.top = space;
        }
    }
}